package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/checkItem")
@ResponseBody
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        System.out.println(22);
        Result result = new Result(false,null);
        try {
            checkItemService.addCheckItem(checkItem);
            result.setFlag(true);
            result.setMessage(MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            result.setMessage(MessageConstant.ADD_CHECKITEM_FAIL);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Result result=checkItemService.findPage(queryPageBean);
        return result;
    }

    @RequestMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        boolean flag=false;
        String message=null;
        try {
            checkItemService.update(checkItem);
            flag=true;
            message=MessageConstant.EDIT_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.EDIT_CHECKITEM_FAIL;
            e.printStackTrace();
        }

        return new Result(flag,message);
    }

    @RequestMapping("/del")
    public Result del(Integer id){
        boolean flag=false;
        String message=null;
        try {
            checkItemService.del(id);
            flag=true;
            message=MessageConstant.DELETE_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            flag=false;
            message=MessageConstant.DELETE_CHECKITEM_FAIL;
            e.printStackTrace();
        }

        return new Result(flag,message);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        boolean flag=false;
        String message=null;
        CheckItem checkItem= null;
        try {
            checkItem = checkItemService.findById(id);
            flag=true;
            message=MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
            e.printStackTrace();
        }

        return new Result(flag,message,checkItem);
    }

    @RequestMapping("/findAllCheckItem")
    public Result findAll(){
        boolean flag=false;
        String message=null;
        List<CheckItem>checkItemList= null;
        try {
            checkItemList = checkItemService.findAllCheckItem();
            flag=true;
            message=MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,checkItemList);
    }
}
